﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Md.Api.DDD;
using Md.Api.Model.Util.Sms.Request;
using Newtonsoft.Json;

namespace Md.Util.Service.Sms;
/// <summary>
/// 阿里云国内短信服务
/// 阿里云短信对接文档：https://help.aliyun.com/zh/sms/developer-reference/dotnet-sdk?spm=a2c4g.11186623.0.0.385647a2Xqz5u4
/// 地域：https://api.aliyun.com/product/Dysmsapi
/// </summary>
public class AliSendSmsService : ApiBase<RequestAliSendSmsModel>
{
    protected override void ExecuteMethod()
    {
        IClientProfile profile = DefaultProfile.GetProfile(JsonConfig.JsonRead("MdAli:SMS:Region"), JsonConfig.JsonRead("MdAli:AccessKeyId"), JsonConfig.JsonRead("MdAli:AccessSecret"));

        DefaultAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();

        request.Method = MethodType.POST;

        request.Domain = JsonConfig.JsonRead("MdAli:SMS:Endpoint");

        request.Version = DateTime.Now.ToString("yyyy-MM-dd");

        request.Action = "SendSms";

        // request.Protocol = ProtocolType.HTTP;

        request.AddQueryParameters("PhoneNumbers", Parameter.PhoneNumber); //接收短信的手机号码

        request.AddQueryParameters("SignName", JsonConfig.JsonRead("MdAli:SMS:SingName")); //短信签名名称

        request.AddQueryParameters("TemplateCode", Parameter.TemplateCode); //短信模板CODE

        if (Parameter.TemplateParamList != null && Parameter.TemplateParamList.Count() > 0)
        {
            var paramDic = new Dictionary<string, string>();

            foreach (var item in Parameter.TemplateParamList)
            {
                paramDic.Add(item.key, item.value);
            }

            request.AddQueryParameters("TemplateParam", JsonConvert.SerializeObject(paramDic)); //短信模板变量对应的实际值，JSON格式
        }
        

        try
        {
            CommonResponse response = client.GetCommonResponse(request);

            var res = JsonConvert.DeserializeObject<AliSmsResponseModel>(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));

            if (res == null)
            {
                throw new AggregateException("短信发送异常");
            }

            if (res.Code == "OK" && res.Message == "OK")
            {
                var uuid = Guid.NewGuid().ToString();
                Result.Data = uuid;
                Result.Msg = "success";
            }
            else
            {
                throw new AggregateException(res.Message);
            }
        }
        catch (ServerException e)
        {
            throw new AggregateException(e.Message);
        }
        catch (ClientException e)
        {
            throw new AggregateException(e.Message);
        }
    }
}
/// <summary>
/// 阿里云发送短信响应实体
/// </summary>
public class AliSmsResponseModel
{
    /// <summary>
    /// 请求ID
    /// </summary>
    public string? RequestId { get; set; }
    /// <summary>
    /// 响应内容
    /// </summary>
    public string? Message { get; set; }
    /// <summary>
    /// 业务ID
    /// </summary>
    public string? BizId { get; set; }
    /// <summary>
    /// 响应CODE
    /// </summary>
    public string? Code { get; set; }
}

